﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace 采集卡配置
{
    public partial class Form1 : Form
    {
        public delegate void UpdateText(byte  CMD,byte[] msg);
        public UpdateText updateText;
        int port=5050;
        public IPAddress card_iP;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            updateText = new UpdateText(MyUpdateText);
            openudp();
            textBox_mac.Text=ini.get_ini_val("CONFIG", "MAC");//
            textBox_ID.Text = ini.get_ini_val("CONFIG", "ID");
        }
 Socket server;
        private void openudp()
        {
            string ip = IPAddress.Any.ToString();
        //  textBox_IP.Text = ip;
       
        /// <summary>
        /// 获取本地IP
        /// </summary>

                //1.创建服务器端电话
                server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //2.创建手机卡
                IPAddress iP = IPAddress.Parse(ip);
                IPEndPoint endPoint = new IPEndPoint(iP, 1020);
                //3.将电话卡插进电话中(绑定端口号和IP)
                server.Bind(endPoint);
                listBox1.Items.Add("服务器已经成功开启!");
                //开启接收消息线程
                Thread t = new Thread(ReciveMsg);
                t.IsBackground = true;
                t.Start();
            }
        /// <summary>
        /// 向特定ip的主机的端口发送数据
        /// </summary>te
        void SendMsg_restart()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            server.SendTo(data, point);
        }
        void SendMsg_ID()
            {
                EndPoint point = new IPEndPoint(card_iP, port);
                
                byte[] data = {0x55, 0x0A,0x00,0x00,0x00,0x09, 0x00, 0x00, 0x00, 0x00 };
                server.SendTo(data, point);
            }

        void SendMsg_MAC()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x00, 0x0D, 0x1D, 0x01, 0xA8, 0xC0, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            server.SendTo(data, point);
        }
        void SendMsg_writeMAC()
        {
            //string hostName = Dns.GetHostName();   //获取本机名
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            //IPAddress localaddr = localhost.AddressList[0];
            // EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.40"), 5050);

            EndPoint point = new IPEndPoint(card_iP, port);
            //  byte[] IP = IPAddress.Parse(textBox_mac.Text).GetAddressBytes();
            string[] byteitem = textBox_mac.Text.Replace("-", " ").Trim().Split(' ');
            List<byte> lstByte = new List<byte>();
            foreach (string item in byteitem)
                lstByte.Add(Convert.ToByte(item, 16));
            byte[] array = lstByte.ToArray();
            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x01, 0x0D, 0x1D, 0x01, 0xA8, 0xC0, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            Array.Copy(array, 0, data, 6, 6);
            server.SendTo(data, point);
        }
        void SendMsg_writeid()
        {
            //string hostName = Dns.GetHostName();   //获取本机名
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            //IPAddress localaddr = localhost.AddressList[0];
            // EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.40"), 5050);

            EndPoint point = new IPEndPoint(card_iP, port);
            UInt32 VALID= Convert.ToUInt32(textBox_ID.Text);
            byte[] byteitem = System.BitConverter.GetBytes(VALID);
            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x09, 0x1D, 0x01, 0xA8, 0xC0};
            Array.Copy(byteitem, 0, data, 6, 4);
            server.SendTo(data, point);
        }
        void SendMsg_IP()
        {      ////55 14 00 00 00 0A 1D 01 A8 C0 00 FF FF FF 1E 01 A8 C0 00 00
            //string hostName = Dns.GetHostName();   //获取本机名
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            //IPAddress localaddr = localhost.AddressList[0];
           // textBox_IP.Text;
          //  EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.40"), 5050);
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] IP = IPAddress.Parse(textBox_newIP.Text).GetAddressBytes();
            Byte[] ZWYM = IPAddress.Parse(textBox_zwym.Text).GetAddressBytes(); 
           Byte[] DNS = IPAddress.Parse(textBox_dns.Text).GetAddressBytes(); 

            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x00, 0x0A, 0x1D, 0x01, 0xA8, 0xC0, 0x00, 0xFF, 0xFF, 0xFF, 0x1E, 0x01, 0xA8, 0xC0, 0x00, 0x00 };
            Array.Copy(IP,0, data, 6, 4);
            Array.Copy(ZWYM, 0, data, 10, 4);
            Array.Copy(DNS, 0, data, 14, 4);
            server.SendTo(data, point);
        }
        void SendMsg_mqtt_config()
        {      ////55 14 00 00 00 0A 1D 01 A8 C0 00 FF FF FF 1E 01 A8 C0 00 00
               //string hostName = Dns.GetHostName();   //获取本机名
               //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
               //IPAddress localaddr = localhost.AddressList[0];
               // textBox_IP.Text;
               //  EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.40"), 5050);
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] IP = IPAddress.Parse(textBox_newIP.Text).GetAddressBytes();
            Byte[] ZWYM = IPAddress.Parse(textBox_zwym.Text).GetAddressBytes();
            Byte[] DNS = IPAddress.Parse(textBox_dns.Text).GetAddressBytes();
            string ProductKey_str = textBox_ProductKey.Text;
            string DeviceName_str = textBox_DeviceName.Text;
            string DeviceSecret_str = textBox_DeviceSecret.Text;
            string data_str = "{"+ "ProductKey_str:"+ ProductKey_str+","+ "DeviceName_str:" + DeviceName_str + "," + "DeviceSecret_str:" + DeviceSecret_str+"}";
            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x01, 0x10 };
            byte[] data_MQTT = new Byte[data.Length + data_str.Length];
            Array.Copy(data, 0, data_MQTT,0,6);
           Array.Copy(System.Text.Encoding.Default.GetBytes(data_str), 0, data_MQTT, 6, data_str.Length);

            server.SendTo(data_MQTT, point);
        }
        void GetMsg_mqtt_config()
        {      ////55 14 00 00 00 0A 1D 01 A8 C0 00 FF FF FF 1E 01 A8 C0 00 00
               //string hostName = Dns.GetHostName();   //获取本机名
               //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
               //IPAddress localaddr = localhost.AddressList[0];
               // textBox_IP.Text;
               //  EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.40"), 5050);
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] IP = IPAddress.Parse(textBox_newIP.Text).GetAddressBytes();
            Byte[] ZWYM = IPAddress.Parse(textBox_zwym.Text).GetAddressBytes();
            Byte[] DNS = IPAddress.Parse(textBox_dns.Text).GetAddressBytes();
            string ProductKey_str = textBox_ProductKey.Text;
            string DeviceName_str = textBox_DeviceName.Text;
            string DeviceSecret_str = textBox_DeviceSecret.Text;
            string data_str = "{" + "ProductKey_str:" + ProductKey_str + "," + "DeviceName_str:" + DeviceName_str + "," + "DeviceSecret_str:" + DeviceSecret_str + "}";
            byte[] data = { 0x55, 0x14, 0x00, 0x00, 0x00, 0x10 };
            byte[] data_MQTT = new Byte[data.Length + data_str.Length];
            Array.Copy(data, 0, data_MQTT, 0, 6);
            Array.Copy(System.Text.Encoding.Default.GetBytes(data_str), 0, data_MQTT, 6, data_str.Length);

            server.SendTo(data_MQTT, point);
        }
        float water_gage;
        byte[] water = new byte[4];
        float BATT_V;
        float BATT_A;
        float BATT_max_kulun;
        float BATT_kulun;
        float BATT_temper;
        float BATT_LOW_kulum;
        UInt32 BATT_PCT;
        byte[] BATT = new byte[4];
        byte[] step_position = new byte[4];
        public static float bytesTofloat(byte[] src, int offset = 0)
        {
            float value;
            value = (float)((src[offset] & 0xFF <<24)
                     | ((src[offset + 1] & 0xFF)<< 16 )
                     | ((src[offset + 2] & 0xFF)<< 8)
                     | ((src[offset + 3] & 0xFF)  ));
            return value;
        }
        UInt32 BufToU32(byte[] buf, int offset)
        {
            UInt32 val = 0;
            if ((offset + 4) > buf.Length)
            {
                return 0;
            }
            //buf[offset + 0] = (byte)(num >> 24);
            //buf[offset + 1] = (byte)(num >> 16);
            //buf[offset + 2] = (byte)(num >> 8);
            //buf[offset + 3] = (byte)(num >> 0);

            val |= (UInt32)buf[offset + 0] << 0;
            val |= (UInt32)buf[offset + 1] << 8;
            val |= (UInt32)buf[offset + 2] << 16;
            val |= (UInt32)buf[offset + 3] << 24;

            return val;
        }
        float BufToFloat(byte[] buf, int offset)
        {
            float f = 0;
            UInt32 num = BufToU32(buf, offset);
            byte[] bs = BitConverter.GetBytes(num);
            f = BitConverter.ToSingle(bs, 0);
            return f;
        }
        Int32 BufToint32(byte[] buf, int offset)
        {
            Int32 val = 0;
            if ((offset + 4) > buf.Length)
            {
                return 0;
            }
            //buf[offset + 0] = (byte)(num >> 24);
            //buf[offset + 1] = (byte)(num >> 16);
            //buf[offset + 2] = (byte)(num >> 8);
            //buf[offset + 3] = (byte)(num >> 0);

            val |= (Int32)buf[offset + 0] << 0;
            val |= (Int32)buf[offset + 1] << 8;
            val |= (Int32)buf[offset + 2] << 16;
            val |= (Int32)buf[offset + 3] << 24;

            return val;
        }
        /// <summary>
        /// 接收发送给本机ip对应端口号的数据
        /// </summary>
        void ReciveMsg()
            {

          
            string message ="";
          
                while (true)
                {
                    EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
                    byte[] buffer = new byte[2048];
                    int length = server.ReceiveFrom(buffer, ref point);//接收数据报

                if (length >= 0x0a)
                {
                    switch (buffer[5])
                    {
                        case 0x09:
                            byte[] id = new byte[4];
                            Array.Copy(buffer,6, id,0,4);
                          //  Array.Reverse(MAC);  
                            this.BeginInvoke(updateText, buffer[5], id);
                            break;
                        case 0x0D:
                            byte[] MAC = new byte[6];
                            Array.Copy(buffer, 6, MAC, 0, 6);
                            //  Array.Reverse(MAC);  
                            this.BeginInvoke(updateText, buffer[5], MAC);
                            break;
                        case 0x0A:
                            byte[] data= new byte[12];
                            Array.Copy(buffer, 6, data, 0, 12);
                            //  Array.Reverse(MAC);  
                            this.BeginInvoke(updateText, buffer[5], data);
                            break;
                        case 0x10:
                            byte[] data1 = new byte[70];
                            Array.Copy(buffer, 6, data1, 0, 70);
                            //  Array.Reverse(MAC);  
                            this.BeginInvoke(updateText, buffer[5], data1);
                            break;
                        case 0x55:
                            byte[] data2 = new byte[23];
                            Array.Copy(buffer, 6, data2, 0, 23); 
                            this.BeginInvoke(updateText, buffer[5], data2);
                            break;
                        case 0x56:
                            byte[] data3 = new byte[27];
                            Array.Copy(buffer, 6, data3, 0, 27);
                            this.BeginInvoke(updateText, buffer[5], data3);
                            break;

                        case 0x57:
                            byte[] data4 = new byte[27];
                            Array.Copy(buffer, 6, data4, 0, 27);
                            this.BeginInvoke(updateText, buffer[5], data4);
                            break;
                    }
                }
              //  string message= BitConverter.ToString(buffer);
                  //  string message = Encoding.UTF8.GetString(buffer, 0, length);
                  //  listBox1.Items.Add(point.ToString() + "：" + message);
 
            }
            }



        public void MyUpdateText(byte cmd, byte[] msg)
        {
            UInt32 mac_32;
            switch (cmd)
            {
                case 0x09:
                    mac_32 = BitConverter.ToUInt32(msg, 0);
                  // textBox_ID.Text = mac_32.ToString();
                    listBox1.Items.Add("ID:" + mac_32.ToString());
                    break;
                case 0x0D:
                    
                    //textBox_mac.Text = BitConverter.ToString(msg, 0);
                  
                    listBox1.Items.Add("MAC:" + BitConverter.ToString(msg, 0));
                    break;
                case 0x0A:
                    // BitConverter.ToString(msg, 0);
                    //  textBox_mac.Text = BitConverter.ToString(msg, 0);
                    string str = string.Join(",", msg.Select(t => t.ToString()).ToArray());

                    listBox1.Items.Add("设备IP:"+str);
                    break;
                case 0x10:
                    // BitConverter.ToString(msg, 0);
                    //  textBox_mac.Text = BitConverter.ToString(msg, 0);
                   // string str1 = string.Join(",", msg.Select(t => t.ToString()).ToArray());
                    String s = System.Text.Encoding.Default.GetString(msg, 0, 16);
                    listBox1.Items.Add("ProductKey:" + s + "/r/n");
                    textBox_ProductKey.Text = s;
                   
                    s = System.Text.Encoding.Default.GetString(msg, 16, 16) ;
                    listBox1.Items.Add("DeviceName:" + s + "/r/n");
                    textBox_DeviceName.Text = s;
                    s =  System.Text.Encoding.Default.GetString(msg, 32, 32)  ;
                    listBox1.Items.Add("DeviceSecret:" + s );
                    textBox_DeviceSecret.Text = s;

                    break;
                case 0x55:
                    //  BitConverter.ToString(msg, 0);
                    // textBox1_batt.Text = BitConverter.ToString(msg, 0);

                    if (msg[0] == 1)
                        TB_NOTOR_STA.Text = "移动中";
                    else if (msg[0] == 2)
                        TB_NOTOR_STA.Text = "过载";
                    else if (msg[0] == 4)
                        TB_NOTOR_STA.Text = "超温";

                    else TB_NOTOR_STA.Text = "已停止";

                    Array.Copy(msg, 2, step_position, 0, 4);
                    Int32 p_step_position = BufToint32(step_position, 0);
                    s = p_step_position.ToString();
                    textBox_step_position.Text = s;


                    str = string.Join(" ", msg.Select(t => t.ToString("X2")).ToArray());
                    listBox1.Items.Add("READ :" + str);
                    break;
                case 0x56:
                    Array.Clear(BATT, 0, 4);
                    Array.Copy(msg, 1, BATT, 0, 1);
                    BATT_PCT = BufToU32(BATT, 0);
                    BATT_PCT = (UInt32)Math.Round((double)(BATT_PCT ));//

                    textBox1_batt.Text = BATT_PCT.ToString();//电量

                    Array.Copy(msg, 2, BATT, 0, 4);
                    BATT_V = BufToFloat(BATT, 0);
                    s = string.Format("{0:f2}", BATT_V);//电压
                    textBox_BATT_V.Text = s;

                    Array.Copy(msg, 6, water, 0, 4);
                    BATT_A = BufToFloat(water, 0);
                    s = string.Format("{0:f2}", BATT_A);//电流
                    textBox_BATT_A.Text = s;

                    Array.Copy(msg, 10, water, 0, 4);
                    BATT_max_kulun = BufToFloat(water, 0);
                    s = string.Format("{0:f2}", BATT_max_kulun);//电池容量
                    textBox_BATT_max_kulun.Text = s;

                    Array.Copy(msg, 14, water, 0, 4);
                    BATT_kulun = BufToFloat(water, 0);
                    s = string.Format("{0:f2}", BATT_kulun);//剩余电量
                    textBox_BATT_kulum.Text = s;

                    Array.Copy(msg, 18, water, 0, 4);
                    BATT_temper = BufToFloat(water, 0);
                    s = string.Format("{0:f2}", BATT_temper);//温度
                    textBox_BATT_temper.Text = s;

                    Array.Copy(msg, 22, water, 0, 4);
                    BATT_LOW_kulum = BufToFloat(water, 0);
                    s = string.Format("{0:f2}", BATT_LOW_kulum);//低电量值
                    TB_BATT_LOW_kulum.Text = s;


                    if (msg[26] == 1)
                        textBox_batt_sta.Text = "充电中";
                    else textBox_batt_sta.Text = "放电中";
                    /*
                      Array.Copy(msg, 18, step_position, 0, 4);
                     Int32 p_step_position = BufToint32(step_position, 0);
                      s = p_step_position.ToString();
                      textBox_step_position.Text = s;*/

                    if(checkBox_log.Checked)
                    { 
                    str = string.Join(" ", msg.Select(t => t.ToString("X2")).ToArray());
                    listBox1.Items.Add("RX:" + str);
                     }
                    break;

                case 0x57:
                    Array.Clear(BATT, 0, 4);

                    Array.Copy(msg, 2, step_position, 0, 4);
                     p_step_position = BufToint32(step_position, 0);
                     s = p_step_position.ToString();
                    tb_batt_ing_position.Text = s;

                    Array.Copy(msg, 6, step_position, 0, 4);
                    p_step_position = BufToint32(step_position, 0);
                    s = p_step_position.ToString();
                    tb_Axis_head_position.Text = s;

                    Array.Copy(msg, 10, step_position, 0, 4);
                    p_step_position = BufToint32(step_position, 0);
                    s = p_step_position.ToString();
                    tb_Axis_end_position.Text = s;

                    if (checkBox_log.Checked)
                    {
                        str = string.Join(" ", msg.Select(t => t.ToString("X2")).ToArray());
                        listBox1.Items.Add("RX:" + str);
                    }
                    break;
                    ;
                case 0x88:
                    if (checkBox_log.Checked)
                    {
                        str = string.Join(" ", msg.Select(t => t.ToString("X2")).ToArray());
                        listBox1.Items.Add("TX:" + str);
                    }
                    break;



            }
        }

        private void button_READ_MAC_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tMAC = new Thread(SendMsg_MAC);
            tMAC.Start();
        }
        

        private void button_READ_ID_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                card_iP = IPAddress.Parse(textBox_IP.Text);
                
                //开启发送消息线程
                Thread t2 = new Thread(SendMsg_ID);
                t2.Start();
            }
        }

        private void button_READ_IP_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_IP);
            tIP.Start();
        }
        ch_config USB_CH_CONFIG = new ch_config();
        private void button_write_mac_Click(object sender, EventArgs e)
        {

            card_iP = IPAddress.Parse(textBox_IP.Text);
           
           ini. set_ini_val("CONFIG","MAC", textBox_mac.Text);//
            //开启发送消息线程
            Thread twrite_mac = new Thread(SendMsg_writeMAC);
            twrite_mac.Start();
        }

        private void button_write_id_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            ini.set_ini_val("CONFIG", "ID", textBox_ID.Text);//
            //开启发送消息线程
            Thread twrite_mac = new Thread(SendMsg_writeid);
            twrite_mac.Start();
        }

        private void button_restart_Click(object sender, EventArgs e)
        {
            SendMsg_restart();
            listBox1.Items.Add("设备重启" );
        }

        private void button_read_mqtt_config_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(GetMsg_mqtt_config);
            tIP.Start();
        }

        private void button_w_mqtt_config_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_mqtt_config);
            tIP.Start();
        }

        private void textBox_IP_TextChanged(object sender, EventArgs e)
        {

        }
        void SendMsg_M1_ON()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0x01, 90, 180, 0x00 };
            server.SendTo(data, point);
        }
        void SendMsg_M2_ON()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0x02, 180, 90, 0x00 };
            server.SendTo(data, point);
        }
        void SendMsg_M_OFF()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0x00, 0, 0x00, 0x00 };
            server.SendTo(data, point);
        }
        private void BT_M1_ON_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_M1_ON);
            tIP.Start();
        }

        private void BT_M2_ON_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_M2_ON);
            tIP.Start();
        }

        private void BT_M_STOP_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_M_OFF);
            tIP.Start();
        }
        void SendMsg_get_motor()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x00, 0x55 };
            server.SendTo(data, point);
        }
        void SendMsg_READ_batt()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x00, 0x56 };
            server.SendTo(data, point);
        }

        void SendMsg_READ_motor_Preset_point()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x00, 0x57 };
            server.SendTo(data, point);
        }

        void P_STEPMOTOR_AxisMoveRel()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00,0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            data[6] = 1;
            data[7] = 2;
            Int32 pulls = Int32.Parse(textBox_jiaodu.Text) * Int32.Parse(textBox_jiaodu2.Text);
            data[8] = (byte)(pulls);
            data[9] = (byte)(pulls >> 8);
            data[10] = (byte)(pulls >> 16);
            data[11] = (byte)(pulls >> 24);

            UInt32 ipt = UInt32.Parse(M_add.Text);
            data[12] = (byte)(ipt);
            data[13] = (byte)(ipt >> 8);
            data[14] = (byte)(ipt >> 16);
            data[15] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_sub.Text);
            data[16] = (byte)(ipt);
            data[17] = (byte)(ipt >> 8);
            data[18] = (byte)(ipt >> 16);
            data[19] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_max.Text);
            data[20] = (byte)(ipt);
            data[21] = (byte)(ipt >> 8);
            data[22] = (byte)(ipt >> 16);
            data[23] = (byte)(ipt >> 24);
            server.SendTo(data, point);
        }
        void send_batt_data()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x56, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            // data[7] = Convert.ToByte(textBox_jiaodu.Text);
            // data[8] = Convert.ToByte(textBox_jiaodu2.Text);
            //data[6] = 2;

            float BATT_max_kulun = float.Parse(textBox_BATT_max_kulun.Text);
            byte[] b = BitConverter.GetBytes(BATT_max_kulun);
            data[7] = b[0];
            data[8] = b[1];
            data[9] = b[2];
            data[10] = b[3];

            float BATT_kulum = float.Parse(textBox_BATT_kulum.Text);
             b = BitConverter.GetBytes(BATT_kulum);
            data[11] = b[0];
            data[12] = b[1];
            data[13] = b[2];
            data[14] = b[3];
            float BATT__LOW_kulum = float.Parse(TB_BATT_LOW_kulum.Text);
            b = BitConverter.GetBytes(BATT__LOW_kulum);
            data[15] = b[0];
            data[16] = b[1];
            data[17] = b[2];
            data[18] = b[3];


            byte[] data3 = new byte[27];
            Array.Copy(data, 0, data3, 0, 27);
            
            server.SendTo(data, point);
            data[5] = 0X88;
            this.BeginInvoke(updateText, data[5], data3);
        }
        void set_Preset_point()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x56, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            // data[7] = Convert.ToByte(textBox_jiaodu.Text);
            // data[8] = Convert.ToByte(textBox_jiaodu2.Text);
            data[5] = 0x57;
            data[6] = 2;

            Int32 BATT_max_kulun = Int32.Parse(tb_batt_ing_position.Text);
            byte[] b = BitConverter.GetBytes(BATT_max_kulun);
            data[7] = b[0];
            data[8] = b[1];
            data[9] = b[2];
            data[10] = b[3];

             BATT_max_kulun = Int32.Parse(tb_Axis_head_position.Text);
             b = BitConverter.GetBytes(BATT_max_kulun);
            data[11] = b[0];
            data[12] = b[1];
            data[13] = b[2];
            data[14] = b[3];
             BATT_max_kulun = Int32.Parse(tb_Axis_end_position.Text);
             b = BitConverter.GetBytes(BATT_max_kulun);
            data[15] = b[0];
            data[16] = b[1];
            data[17] = b[2];
            data[18] = b[3];




            byte[] data3 = new byte[27];
            Array.Copy(data, 0, data3, 0, 27);

            server.SendTo(data, point);
            data[5] = 0X88;
            this.BeginInvoke(updateText, data[5], data3);
        }
        void P_AxisMoveAbs()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00,0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            data[6] = 1;
            data[7] = 1;

            Int32 pulls = Int32.Parse(textBox_jiaodu.Text) * Int32.Parse(textBox_jiaodu2.Text);
            data[8] = (byte)(pulls);
            data[9] = (byte)(pulls >> 8);
            data[10] = (byte)(pulls >> 16);
            data[11] = (byte)(pulls >> 24);

            UInt32 ipt = UInt32.Parse(M_add.Text);
            data[12] = (byte)(ipt);
            data[13] = (byte)(ipt >> 8);
            data[14] = (byte)(ipt >> 16);
            data[15] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_sub.Text);
            data[16] = (byte)(ipt);
            data[17] = (byte)(ipt >> 8);
            data[18] = (byte)(ipt >> 16);
            data[19] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_max.Text);
            data[20] = (byte)(ipt);
            data[21] = (byte)(ipt >> 8);
            data[22] = (byte)(ipt >> 16);
            data[23] = (byte)(ipt >> 24);
            server.SendTo(data, point);
        }
    void AxisHome()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

    byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            data[7] = 0;
            server.SendTo(data, point);
        }
        void EXTI_IRQHandler_Zero()
        {
            EndPoint point = new IPEndPoint(card_iP, port);

            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 ,0x00};

            data[7] = 4;
            server.SendTo(data, point);
        }

        
        void set_motor_stop()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            // data[7] = Convert.ToByte(textBox_jiaodu.Text);
            // data[8] = Convert.ToByte(textBox_jiaodu2.Text);
            data[7] = 5;
            server.SendTo(data, point);
        }
        void goto_batt_point()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x57, 0xff, 90, 0x00, 0x00,0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            data[5] = 0x55;
            data[6] = 1;
            data[7] = 6;
            Int32 pulls = Int32.Parse(textBox_jiaodu.Text) * Int32.Parse(textBox_jiaodu2.Text);
            data[8] = (byte)(pulls);
            data[9] = (byte)(pulls >> 8);
            data[10] = (byte)(pulls >> 16);
            data[11] = (byte)(pulls >> 24);

            UInt32 ipt = UInt32.Parse(M_add.Text);
            data[12] = (byte)(ipt);
            data[13] = (byte)(ipt >> 8);
            data[14] = (byte)(ipt >> 16);
            data[15] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_sub.Text);
            data[16] = (byte)(ipt);
            data[17] = (byte)(ipt >> 8);
            data[18] = (byte)(ipt >> 16);
            data[19] = (byte)(ipt >> 24);
            ipt = UInt32.Parse(M_max.Text);
            data[20] = (byte)(ipt);
            data[21] = (byte)(ipt >> 8);
            data[22] = (byte)(ipt >> 16);
            data[23] = (byte)(ipt >> 24);
            server.SendTo(data, point);
        }
        void set_motor_reset()
        {
            EndPoint point = new IPEndPoint(card_iP, port);
            byte[] data = { 0x55, 0x0A, 0x00, 0x00, 0x01, 0x55, 0xff, 90, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00, 0x00, 0x55, 0xff, 90, 0x00, 0x00, 0x00 };
            // data[7] = Convert.ToByte(textBox_jiaodu.Text);
            // data[8] = Convert.ToByte(textBox_jiaodu2.Text);
            data[7] = 0xfe;
            server.SendTo(data, point);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "相对位置";
           card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(P_STEPMOTOR_AxisMoveRel); 
            tIP.Start(); 

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void BT_P_AxisMoveAbs_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "绝对位置";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(P_AxisMoveAbs); 
            tIP.Start();

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "执行中";
            TB_NOTOR_STA.Text = "执行中";
            TB_NOTOR_STA.Text = "执行中";
            TB_NOTOR_STA.Text = "执行中";
            TB_NOTOR_STA.Text = "执行中";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(EXTI_IRQHandler_Zero); 
            tIP.Start();
        }

        private void button_AxisHome_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "归零点";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(AxisHome);
            tIP.Start();
        }

        private void bt_read_batt_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_READ_batt);
            tIP.Start();
        }

        private void bt_set_batt_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(send_batt_data);
            tIP.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time_OE.Checked)
            {
                card_iP = IPAddress.Parse(textBox_IP.Text);
                //开启发送消息线程
                Thread tIP = new Thread(SendMsg_READ_batt);
                tIP.Start();
            }
        }

        private void time_OE_Click(object sender, EventArgs e)
        {
            if (time_OE.Checked)
            {
                timer1.Enabled = true;
            }
            else {
                timer1.Enabled = false;
            }
                
        }

        private void checkBox_log_Click(object sender, EventArgs e)
        {

        }

        private void bt_log_clear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void BT_GET_MOTOR_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_get_motor);
            tIP.Start();
        }

        private void bt_motor_stop_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "执行中";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(set_motor_stop);
            tIP.Start();
        }

        private void bt_set_Preset_point_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(set_Preset_point);
            tIP.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "去充电";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(goto_batt_point);
            tIP.Start();
        }

        private void bt_get_Preset_point_Click(object sender, EventArgs e)
        {
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(SendMsg_READ_motor_Preset_point);
            tIP.Start();

        }

        private void bt_vout_rest_Click(object sender, EventArgs e)
        {
            TB_NOTOR_STA.Text = "重置";
            card_iP = IPAddress.Parse(textBox_IP.Text);
            //开启发送消息线程
            Thread tIP = new Thread(set_motor_reset);
            tIP.Start();

        }
    }

}